﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlClient;

namespace IHISCETeim.DAL
{
    public class OFactoryRenovationDAL
    {
        public static int Add(OFactoryRenovationInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OFactoryRenovation_Add");
            DB.AddInParameter(dbCommand, "DocNum", DbType.String, obj.DocNum);
            DB.AddInParameter(dbCommand, "IssueDate", DbType.DateTime, obj.IssueDate);
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
            DB.AddInParameter(dbCommand, "DemandDept", DbType.String, obj.DemandDept);
            DB.AddInParameter(dbCommand, "ReturnDate", DbType.DateTime, obj.ReturnDate);
            DB.AddInParameter(dbCommand, "MachineProperty", DbType.String, obj.MachineProperty);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
            DB.AddInParameter(dbCommand, "Num", DbType.Int32, obj.Num);
            DB.AddInParameter(dbCommand, "WorkTime", DbType.String, obj.WorkTime);
            DB.AddInParameter(dbCommand, "RenovationContent", DbType.String, obj.RenovationContent);
            DB.AddInParameter(dbCommand, "IsCharges", DbType.Boolean, obj.IsCharges);
            DB.AddInParameter(dbCommand, "BuyBackCost", DbType.Decimal, obj.BuyBackCost);
            DB.AddInParameter(dbCommand, "Freight", DbType.Decimal, obj.Freight);
            DB.AddInParameter(dbCommand, "IsThreeParty", DbType.Boolean, obj.IsThreeParty);
            DB.AddInParameter(dbCommand, "IsInvoice", DbType.Boolean, obj.IsInvoice);
            DB.AddInParameter(dbCommand, "BuyRemark", DbType.String, obj.BuyRemark);

            //return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OFactoryRenovation_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetList(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OFactoryRenovation_GetList", ID);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTableCollection GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OFactoryRenovation_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTableCollection dt = DB.ExecuteDataSet(dbCommand).Tables;

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
        public static bool UpdateThreePartyFile(int id, string ThreePartyFile)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OFactoryRenovation_UpdateThreePartyFile");
            DB.AddInParameter(dbCommand, "ID", DbType.Int32, id);
            DB.AddInParameter(dbCommand, "ThreePartyFile", DbType.String, ThreePartyFile);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static bool Update(OFactoryRenovationInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OFactoryRenovation_Update");
            DB.AddInParameter(dbCommand, "ID", DbType.Int32, obj.ID);           

            //DB.AddInParameter(dbCommand, "IssueDate", DbType.DateTime, obj.IssueDate);
            SqlParameter _IssueDate = new SqlParameter("@IssueDate", SqlDbType.DateTime);
            _IssueDate.IsNullable = true;
            if (obj.IssueDate == DateTime.MinValue)
                _IssueDate.Value = DBNull.Value;
            else
                _IssueDate.Value = obj.IssueDate;
            dbCommand.Parameters.Add(_IssueDate);

            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, obj.SerialNumber);
            DB.AddInParameter(dbCommand, "DemandDept", DbType.String, obj.DemandDept);

            //DB.AddInParameter(dbCommand, "ReturnDate", DbType.DateTime, obj.ReturnDate);
            SqlParameter _ReturnDate = new SqlParameter("@ReturnDate", SqlDbType.DateTime);
            _ReturnDate.IsNullable = true;
            if (obj.ReturnDate == DateTime.MinValue)
                _ReturnDate.Value = DBNull.Value;
            else
                _ReturnDate.Value = obj.ReturnDate;
            dbCommand.Parameters.Add(_ReturnDate);

            DB.AddInParameter(dbCommand, "MachineProperty", DbType.String, obj.MachineProperty);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String, obj.AgentName);
            DB.AddInParameter(dbCommand, "Num", DbType.Int32, obj.Num);
            DB.AddInParameter(dbCommand, "WorkTime", DbType.String, obj.WorkTime);
            DB.AddInParameter(dbCommand, "RenovationContent", DbType.String, obj.RenovationContent);
            DB.AddInParameter(dbCommand, "IsCharges", DbType.Boolean, obj.IsCharges);
            DB.AddInParameter(dbCommand, "BuyBackCost", DbType.Decimal, obj.BuyBackCost);
            DB.AddInParameter(dbCommand, "Freight", DbType.Decimal, obj.Freight);
            DB.AddInParameter(dbCommand, "IsThreeParty", DbType.Boolean, obj.IsThreeParty);
            DB.AddInParameter(dbCommand, "IsInvoice", DbType.Boolean, obj.IsInvoice);
            DB.AddInParameter(dbCommand, "BuyRemark", DbType.String, obj.BuyRemark);
            DB.AddInParameter(dbCommand, "CompDate", DbType.String, obj.CompDate);
            //SqlParameter _CompDate = new SqlParameter("@CompDate", SqlDbType.DateTime);
            //_CompDate.IsNullable = true;
            //if (obj.CompDate == DateTime.MinValue)
            //    _CompDate.Value = DBNull.Value;
            //else
            //    _CompDate.Value = obj.CompDate;
            //dbCommand.Parameters.Add(_CompDate);

            DB.AddInParameter(dbCommand, "CompWorkTime", DbType.String, obj.CompWorkTime);
            DB.AddInParameter(dbCommand, "CompRemark", DbType.String, obj.CompRemark);
            DB.AddInParameter(dbCommand, "NewAgentName", DbType.String, obj.NewAgentName);
            DB.AddInParameter(dbCommand, "NewContractNo", DbType.String, obj.NewContractNo);
            DB.AddInParameter(dbCommand, "NewContractAmount", DbType.Decimal, obj.NewContractAmount);
            DB.AddInParameter(dbCommand, "CostPart", DbType.Decimal, obj.CostPart);
            DB.AddInParameter(dbCommand, "CostYY", DbType.Decimal, obj.CostYY);
            DB.AddInParameter(dbCommand, "CostOldPieces", DbType.Decimal, obj.CostOldPieces);
            DB.AddInParameter(dbCommand, "CostOther", DbType.Decimal, obj.CostOther);
            DB.AddInParameter(dbCommand, "CostTotal", DbType.Decimal, obj.CostTotal);
            //DB.AddInParameter(dbCommand, "InVote", DbType.DateTime, obj.InVoiceDate);
            SqlParameter _InVoiceDate = new SqlParameter("@InVoiceDate", SqlDbType.DateTime);
            _InVoiceDate.IsNullable = true;
            if (obj.InVoiceDate == DateTime.MinValue)
                _InVoiceDate.Value = DBNull.Value;
            else
                _InVoiceDate.Value = obj.InVoiceDate;
            dbCommand.Parameters.Add(_InVoiceDate);

            DB.AddInParameter(dbCommand, "InPart", DbType.Decimal, obj.InPart);
            DB.AddInParameter(dbCommand, "InWork", DbType.Decimal, obj.InWork);
            DB.AddInParameter(dbCommand, "InTotal", DbType.Decimal, obj.InTotal);
            DB.AddInParameter(dbCommand, "InVoucher", DbType.String, obj.InVoucher);
            DB.AddInParameter(dbCommand, "Loss", DbType.Decimal, obj.Loss);
            DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);

            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
